package com.bunny.hsu.playground;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.bunny.hsu.playground.http.httpProvider;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    final int TOTAL_POKEMON = 26;

    //Okhttp Test
    com.bunny.hsu.playground.http.httpProvider provider = new httpProvider();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //绑定listview，并用hashmap将资源id映射到key值上

        ListView list = (ListView) findViewById(R.id.Listview);
        Resources res = getResources();
        ArrayList<HashMap<String , Object>> listItem = new ArrayList<>();

        for (int i = 1 ; i < TOTAL_POKEMON + 1 ; i ++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("Image", res.getIdentifier("p" + i, "drawable", getPackageName()));
            map.put("Name", res.getString
                    (
                            res.getIdentifier("name" + i, "string", getPackageName())
                    )
            );
            map.put("Number", "#" + i);

            listItem.add(map);
        }

        //

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("MSG",provider.post());
            }
        },300);


        //适配器适配listview

        SimpleAdapter listAdapter = new SimpleAdapter(this,listItem,R.layout.list_adapter,new String[]{"Image","Name","Number"},
                new int[]{R.id.ItemImage,R.id.ItemName,R.id.ItemNumber});
        list.setAdapter(listAdapter);


        //监听事件

        final Context context = this.getApplicationContext();

        final AlertDialog ad_1 = new AlertDialog.Builder(this).
                setIcon(R.drawable.p1).
                setMessage(provider.post()).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                }).
                setTitle("#1 妙蛙种子 Bulbasaur").create();

        final AlertDialog ad_2 = new AlertDialog.Builder(this).
                setIcon(R.drawable.p2).
                setMessage("属性：草 毒\n特性：茂盛 叶绿素\n雌雄比例：87.5%:12.5%\n孵化周期：5355步\n" +
                        "属性值：\n生命：60\n攻击：62\n防御：63\n特攻：80\n特防：80\n速度：60\n总和：405").
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setTitle("#2 妙蛙草 Ivysaur").create();


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.v("main", String.valueOf(position));
                switch (position) {
                    case 0:

                        ad_1.show();
                        break;


                    case 1:

                        ad_2.show();
                        break;

                }

            }
        });

    }


}
